<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <link href="style3.css" rel="stylesheet" type="text/css" />
    </head>
    <style type="text/css">
        /*        #Status                  {margin-left: 0px; top: 240px; width: 100px; height:0px; position: inherit}*/
        /*        #SetPerPage              {margin-left: 250px; top: 0px; width: 150px; height:15px; position: relative}*/
        #JumpToPage              {margin-left: 500px; top: -28px; width: 150px; height:15px; position: relative}
        #Previous                {margin-left: 250px; top: 300px; width: 150px; height:15px; position: inherit}
        #PreviousInactive        {margin-left: 250px; top: 300px; width: 150px; height:15px; position: inherit}
        #Next                    {margin-left: 500px; top: 28px; width: 150px; height:15px; position: relative}
        #NextInactive            {margin-left: 500px; top: 28px; width: 150px; height:15px; position: relative}
    </style>

    <?php
    error_reporting(0);

//Connect to database
    $conn = mysql_connect('localhost', 'root', '') or die(mysql_error());
    mysql_select_db("mydb", $conn) or die(mysql_error());

    $sort = ( isset($_GET['sort']) ) ? (string) $_GET[sort] : 'alpha';
    $place = ( isset($_GET['place']) ) ? (string) $_GET[place] : null;
    $area = (isset($_GET['area']) ) ? (string) $_GET[area] : null;
//echo $area;

    $value = mysql_query("SELECT area_of_expertise_id
                        FROM area_of_expertise 
                        WHERE area_of_expertise_text = '{$area}'") Or die(mysql_error());

    $new = mysql_fetch_assoc($value);
    $x = $new['area_of_expertise_id'];
//echo $x;
//query to get the ID of the area of expert. chosen




    session_start();
    $member_id = $_SESSION['member_id'];
//Author: Omar, Intiating per page to 5 conferences per page


    if ($sort == 'alpha') {
        $sorting = 'long_name';
    } else {
        $sorting = 'conference_date';
    }

    if ($place == null) {
        $placing = 1;
    } else {
        $placing = "conference.place= '{$place}'";
    }
    if ($area == null || $area == 'All') {
        $areaing = 1;
    } else {
        $areaing = "conference.area_of_expertise = '{$x}'";
    }
    if ($_GET['pp'] > 0 || $_GET['pp'] == Null) {
        $pp = ( isset($_GET['pp']) ) ? (int) $_GET[pp] : 5;
    } else {
        echo '<script type="text/javascript">', 'alert("Error: You can not set the view per page to this value!")', '</script>';
        $pp = 5;
    }
//Author: Omar, Calculating the total number of pages needed to view the information
    $result = mysql_query("SELECT conference_id, long_name 
                        FROM conference
                        WHERE $placing AND $areaing
                        ORDER BY $sorting") Or die(mysql_error());

    $users_nm = mysql_num_rows($result);
    $total_pages = (int) ceil($users_nm / $pp);

//Author: Omar, Intiating the initial page and handling inputs exceptions
    if ($_GET['p'] <= $total_pages && $_GET['p'] > 0 || $_GET['p'] == Null) {
        $p = ( isset($_GET['p']) ) ? (int) $_GET[p] : 1;
    } else {
        if ($_GET['p'] <= 0 && $_GET['p'] != null) {
            $p = 1;
            echo '<script type="text/javascript">', 'alert("Error: You Can Not Jump to the Specified Page")', '</script>';
        } else {
            $p = $total_pages;
            echo '<script type="text/javascript">', 'alert("Error: You Can Not Jump to the Specified Page")', '</script>';
        }
    }
    $start = (int) ( $p - 1 ) * $pp;
    $result = mysql_query("SELECT conference_id, long_name, conference_date
                        FROM conference
                        WHERE $placing AND $areaing
                        ORDER BY $sorting
                        LIMIT $start, $pp") Or die(mysql_error());
    echo '<a href="conferenceSetup.php">Start a Conference</a><br><br>
        <table width="100%">
<tr>
<td><b>Conference</b>
</td>
<td><b>Date</b>
</td>
</tr>';
    for ($i = 0; $i < $row = mysql_fetch_assoc($result); ++$i) {
        ?>
        <form method="post" action="">
            <?php
            print "<tr>\n";
            print "<td><input type=\"checkbox\" name=\"conf[]\" value=\"{$row['conference_id']}\"><a href='conferencePage.php?conference_id={$row['conference_id']}'>{$row['long_name']}</a</td>\n";
            print "<td>{$row['conference_date']}</td>";
            echo "<td><width=\"0%\" height=\"0\" style=\"display:none\" value=\"\"</td>\n";
        }
        echo '</table>';
        if (isset($_POST['subscribe'])) {
            //an array is created to store all the values of the checkboxes that were clicked
            $num_conf = $_POST['conf'];
            //a variable is initialized to store the length of the array as an integer
            $n = count($num_conf);
            //loops on all the values in the array
            for ($i = 0; $i < $n; $i++) {
                //retrieves the value in the array at position i and stores it in a variable
                $confid = $num_conf[$i];
                //check whether this member is subscribed in this conference as a reviewer or not
                $check_subscriber = mysql_query("SELECT* FROM member_privileges
                 WHERE member_id ='$member_id' AND privileges_id ='4' AND conference_id ='$confid'") or die(mysql_error());
                $row = mysql_fetch_assoc($check_subscriber);
                //if the rows are greater than one then he is a subscriber  in this conference
                if ($row > 1) {
                    echo '<script>', 'alert("your are already subscriber in this conference");', '</script>';
                } else {
                    mysql_query("INSERT INTO member_privileges (member_id, privileges_id ,conference_id)
                        VALUES ('$member_id', '4', '$confid')") or die("error");
                    echo '<script>', 'alert("your action done correctly");', '</script>';
                }
            }
        }
        ?> 

        <body>

            <div id="Subscribe" style="float:right">
                <form action= 'availableConferences.php' method="GET" name="subscribe" enctype="multipart/form-data">
                    <p>
                        <input name="subscribe" type="submit" value=Subscribe>
                    </p>
                    <input name="pp" type="hidden"  value="<?php echo $pp; ?>">
                    <input name="sort" type="hidden"  value= "<?php echo $sort; ?>">
                    <input name="place" type="hidden"  value= "<?php echo $place; ?>">
                    <input name="area" type="hidden"  value= "<?php echo $area; ?>">
                    <input name="p" type="hidden"  value="<?php echo $p; ?>">
                </form>
            </div>
            <div id="SortAlphabetically" style="float:right">
                <form action= 'availableConferences.php' method="GET" name="ViewConferences" enctype="multipart/form-data">
                    <p>
                        <input type="submit" name="sortAlph" value="Sort Alphabetically"  />
                        </br>
                        </br>
                    </p>
                    <input name="pp" type="hidden"  value="<?php echo $pp; ?>">
                    <input name="sort" type="hidden"  value="alpha">
                    <input name="place" type="hidden"  value= "<?php echo $place; ?>">
                    <input name="area" type="hidden"  value= "<?php echo $area; ?>">
                </form>
            </div>
            <div id="SortByDate" style="float:right">
                <form action= 'availableConferences.php' method="GET" name="ViewConferences" enctype="multipart/form-data">
                    <p>
                        <input type="submit" name="sortbydate" value="Sort by date" />
                        </br>
                        </br>
                    </p>
                    <input name="pp" type="hidden"  value="<?php echo $pp; ?>">
                    <input name="sort" type="hidden"  value="date">
                    <input name="place" type="hidden"  value= "<?php echo $place; ?>">
                    <input name="area" type="hidden"  value= "<?php echo $area; ?>">
                </form>
            </div>
            <div id="FilterByPlace" style="float:left">
                <form action= 'availableConferences.php' method="GET" name="ViewConferences" enctype="multipart/form-data">
                    <p>
                        <label> Filter by Place: </label>

<!--                        <input type ="text" name ="place" value="<?php echo $place; ?>" method ="get">-->
                        <?php
                        $places = mysql_query("SELECT DISTINCT place FROM conference");

                        $drop = "<select name='place'>";
                        $drop .= "\r\n<option value='All'>All</option>";
                        while ($row = mysql_fetch_assoc($places)) {
                            $drop .= "\r\n<option value='{$row['place']}'>{$row['place']}</option>";
                        }
                        $drop .= "\r\n</select>";
                        echo $drop;
                        ?>

                        &nbsp<input type="submit" name="submitplace" value="Submit" />
                    </p>
                    <input name="pp" type="hidden"  value="<?php echo $pp; ?>">
                    <input name="sort" type="hidden"  value="<?php echo $sort; ?>">
                    <input name="area" type="hidden"  value= "<?php echo $area; ?>">                    
                </form>
            </div>
            </br>
            </br>
            </br>
            </br>
            </br>
            <div id="FilterByAreaOfExpertise" style="float:left">
                <form action= 'availableConferences.php' method="GET" name="ViewConferences" enctype="multipart/form-data" >
                    <p>
                        <label> Filter by Area of expertise: </label>
                        <?php
                        $all_criteria = mysql_query("SELECT * FROM area_of_expertise");

                        $Area = "<select name='area'>";
                        $Area .= "\r\n<option value='All'>All</option>";
                        while ($row = mysql_fetch_assoc($all_criteria)) {
                            $Area .= "\r\n<option value='{$row['area_of_expertise_text']}'>{$row['area_of_expertise_text']}</option>";
                        }
                        $Area .= "\r\n</select>";
                        echo $Area;

                        mysql_close($conn);
                        ?>

                        &nbsp;
                        <input type="submit" name="submitarea" value="Submit" />
                    </p>
                    <input name="pp" type="hidden"  value="<?php echo $pp; ?>">
                    <input name="sort" type="hidden"  value="<?php echo $sort; ?>">
                    <input name="place" type="hidden"  value= "<?php echo $place; ?>">

                </form>
            </div>
            </br>
            </br>
            </br>
            </br>
            </br>
            <div id="Status">
                <form action= 'availableConferences.php' method="GET" name="SetPerPage" enctype="multipart/form-data">
                    <p><strong>You are currently viewing page </strong> <?PHP echo $p ?> of <?PHP echo $total_pages ?> 
                        <strong>and viewing </strong><?PHP echo $pp ?><strong> conference(s) per page</strong></p>
                </form>
            </div>
            <!--Author: Omar: Creating input form for user to set #conferences per page-->
            <div id="SetPerPage">
                <form action= 'availableConferences.php' method="GET" name="SetPerPage" enctype="multipart/form-data">
                    <p>Items per Page:
                        <input name="pp" size="2" maxlength="100">
                    </p>
                    <p>
                        <input name="Set" type="submit" value="Set">
                        <input name="sort" type="hidden"  value="<?php echo $sort; ?>">
                        <input name="place" type="hidden"  value= "<?php echo $place; ?>">
                        <input name="area" type="hidden"  value= "<?php echo $area; ?>">
                    </p>
                </form>
            </div>
            <!--Author: Omar: Creating input form for user to set jump to a specified page-->
            <div id="JumpToPage">
                <form action= 'availableConferences.php' method="GET" name="JumpToPage" enctype="multipart/form-data">
                    <p>Jump to page:
                        <input name="p" value="<?PHP print $p; ?>" size="5" maxlength="100">
                    </p>
                    <p>
                        <input name="Go" type="submit" value="Go">
                    </p>
                    <input name="pp" type="hidden"  value="<?php echo $pp; ?>">
                    <input name="sort" type="hidden"  value="<?php echo $sort; ?>">
                    <input name="place" type="hidden"  value= "<?php echo $place; ?>">
                    <input name="area" type="hidden"  value= "<?php echo $area; ?>">
                </form>
            </div>
            <?php if ($p < $total_pages): ?>
                <!--Author: Omar: Creating a button to jump to next page-->
                <div id="Next">
                    <form action= 'availableConferences.php' method="GET" name="Next" enctype="multipart/form-data">
                        <p>
                            <input name="Next" type="submit" value="Next Page">
                        </p>
                        <input name="pp" type="hidden"  value="<?php echo $pp; ?>">
                        <input name="p" type="hidden"  value="<?php echo $p + 1; ?>">
                        <input name="sort" type="hidden"  value="<?php echo $sort; ?>">
                        <input name="place" type="hidden"  value= "<?php echo $place; ?>">
                        <input name="area" type="hidden"  value= "<?php echo $area; ?>"></form>
                </div>
            <?php else: ?>
                <!--Author: Omar: Creating an inactive next button if there is no next page-->
                <div id="NextInactive">
                    <form action= 'availableConferences.php' method="GET" name="NextInactive" enctype="multipart/form-data" disabled="disabled">
                        <p>
                            <input name="NextInactive" type="submit" value="Next Page" disabled="disabled">
                        </p>                       
                    <?php endif; ?>
                </form>
            </div>
            <?php if ($p == 1): ?>
                <!--Author: Omar: Creating an inactive previous button if there is no previous page-->
                <div id="PreviousInactive">
                    <form action= 'availableConferences.php' method="GET" name="PreviousInactive" enctype="multipart/form-data" disabled="disabled">
                        <p>
                            <input name="PreviousInactive" type="submit" value="Previous Page" disabled="disabled">
                        </p>
                    </form>
                </div>

            <?php else: ?>
                <!--Author: Omar: Creating a previous button to jump to previous page-->
                <div id="Previous">
                    <form action= 'availableConferences.php' method="GET" name="ViewConferences" enctype="multipart/form-data">
                        <p>
                            <input name="Next" type="submit" value="Previous Page">
                        </p>
                        <input name="pp" type="hidden"  value="<?php echo $pp; ?>">
                        <input name="p" type="hidden"  value="<?php echo $p - 1; ?>">
                        <input name="sort" type="hidden"  value="<?php echo $sort; ?>">
                        <input name="place" type="hidden"  value= "<?php echo $place; ?>">
                        <input name="area" type="hidden"  value= "<?php echo $area; ?>">
                    </form>
                </div>
            <?php endif; ?>
        </body>
</html>